package com.agan.aganalgorithm.test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author agan
 * @ClassName QueueDemo
 * @Description
 * @Date 2021/4/9 10:26 上午
 **/
public class QueueDemo {

    public static void main(String[] args) {
        BlockingQueue<Object> queue = new ArrayBlockingQueue<>(10);


        Runnable producers = () ->{
            while (true) {
                try {
                    Object o = new Object();
                    System.out.println("生产中"+o.hashCode());
                    queue.put(o);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(producers).start();
        new Thread(producers).start();

        Runnable consumer = ()->{
            while (true) {
                try {

                    Object take = queue.take();
                    System.out.println("消费中"+take.hashCode());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(consumer).start();
        new Thread(consumer).start();
    }

}
